import { Pipe, PipeTransform } from '@angular/core';
import { ToolService } from '../service/tool.service';

@Pipe({
  name: 'datestr'
})
export class DatestrPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    if(value==0 || value=='0'){
        return;
    }
   let str = parseInt(value) * 1000;//将php时间戳转化为整形并乘以1000
   let newDate = new Date(str);
   let year = newDate.getFullYear();//取年份
   let month = newDate.getMonth() + 1;//取月份
    let monthStr="";
    if(month<10){
      monthStr="0"+month
    }else{
      monthStr=month+"";
    }
    let nowday = newDate.getDate();//取天数
    let nowdayStr="";
    if (nowday >= 0 && nowday <= 9) {
      nowdayStr = "0" + nowday;
    } else {
      nowdayStr = nowday + "";
    }
    let hour=newDate.getHours();
    let min=newDate.getMinutes();
    let second=newDate.getSeconds();
    if(args=="t"){//只有时分秒
        return hour+":"+min+":"+second;
    }else if(args=="p"){//年月日，时分秒
      return year + "-" + monthStr + "-" + nowdayStr+"  "+hour+":"+min+":"+second;
    }
    return year + "-" + monthStr + "-" + nowdayStr;  //默认是年月日
  }

}
